<?php

namespace App\Http\Controllers\Registry;

use App\Enums\RegistryFileType;
use App\Http\Controllers\Controller;
use App\Services\ObsService;
use App\Services\RegistryService;
use Illuminate\Support\Arr;

class CommonController extends Controller
{
    /** @var RegistryService */
    private $registryService;

    /** @var ObsService */
    private $obsService;

    /**
     * CommonController constructor.
     */
    public function __construct(RegistryService $registryService, ObsService $obsService)
    {
        $this->registryService = $registryService;
        $this->obsService = $obsService;
    }

    /** 上传文件 */
    public function uploadFile()
    {
        request()->validate([
            'file_type' => 'required | enum_value:' . RegistryFileType::class,
        ], [
            'file_type.enum_value' => '不支持的文件类型',
        ]);
        $fileType = RegistryFileType::fromValue(request()->file_type);
        request()->validate([
            'file' => 'file | max:' . $fileType->sizeLimit(),
        ], [
            'file.max' => '文件尺寸超过限制',
        ]);

        $key = sprintf('registry/%s/%s', $fileType->value, uniqid());
        $file = request()->file('file');
        $fileUrl = $this->obsService->upload($key, $file);

        return [
            'file_url' => $fileUrl,
        ];
    }

    /** 获取字典数据 */
    public function getData($key)
    {
        switch ($key) {
            case 'creative_natures':
                return $this->parseDicts($this->registryService->proCreateNat());

                break;

            case 'categories':
                $categories = $this->registryService->proCat();

                return $this->parseCategories($categories);

                break;

            case 'belong_methods':
                return $this->parseDicts($this->registryService->autBelMethod());

                break;

            case 'rights':
                return $this->parseDicts($this->registryService->autHold());

                break;

            case 'accept_methods':
                return $this->parseDicts($this->registryService->autAcpMethod());

                break;

            case 'nations':
                return $this->getNations();

                break;

            default:
                return [];
        }
    }

    /** 查询民族列表 */
    private function getNations()
    {
        $keyValues = [];
        $nations = ['汉族', '壮族', '满族', '回族', '苗族', '维吾尔族', '土家族', '彝族', '蒙古族', '藏族', '布依族', '侗族', '瑶族', '朝鲜族', '白族', '哈尼族', '哈萨克族', '黎族', '傣族', '畲族', '傈僳族', '仡佬族', '东乡族', '高山族', '拉祜族', '水族', '佤族', '纳西族', '羌族', '土族', '仫佬族', '锡伯族', '柯尔克孜族', '达斡尔族', '景颇族', '毛南族', '撒拉族', '布朗族', '塔吉克族', '阿昌族', '普米族', '鄂温克族', '怒族', '京族', '基诺族', '德昂族', '保安族', '俄罗斯族', '裕固族', '乌孜别克族', '门巴族', '鄂伦春族', '独龙族', '塔塔尔族', '赫哲族', '珞巴族'];
        foreach ($nations as $nation) {
            $keyValues[] = [
                'key' => $nation,
                'value' => $nation,
            ];
        }

        return $keyValues;
    }

    /** 产品分类数据转化 */
    private function parseCategories($list)
    {
        $videoCatNames = [
            '电影作品',
            '录像制品',
            '舞蹈作品',
            '杂技艺术作品',
            '类似摄制电影方法创作的作品',
        ];

        $wordCatNames = [
            '口述作品',
            '文字作品',
        ];

        $parsedCategories = [];
        foreach ($list as $item) {
            $name = Arr::get($item, 'name');
            $parsedCategories[] = [
                'key' => $name,
                'value' => $name,
                'description' => Arr::get($item, 'introduce'),
                'is_video' => in_array($name, $videoCatNames),
                'is_word' => in_array($name, $wordCatNames),
            ];
        }

        return $parsedCategories;
    }

    /** 字典数据结构转化 */
    private function parseDicts($list)
    {
        $parsedList = [];
        foreach ($list as $item) {
            $name = Arr::get($item, 'name');
            $parsedList[] = [
                'key' => $name,
                'value' => $name,
            ];
        }

        return $parsedList;
    }
}
